package love.yuanmeng.guetcoalsystem.cotroller;

import love.yuanmeng.guetcoalsystem.pojo.User;
import love.yuanmeng.guetcoalsystem.service.UserService;
import love.yuanmeng.guetcoalsystem.util.DataGridView;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@CrossOrigin
@RequestMapping("admin")
public class AdminController {

    private UserService userService;

    public AdminController(UserService userService) {
        this.userService = userService;
    }

    @ResponseBody
    @GetMapping("getUsers")
    public DataGridView getUsers() {
        return userService.getAllUser();
    }

    @ResponseBody
    @PostMapping("addUser")
    public String addUser(@RequestBody User user) {
        return userService.addUser(user) == 1 ? "SUCCESS" : "ERROR";
    }

    @ResponseBody
    @PostMapping("updateUser")
    public String updateUser(@RequestBody User user) {
        return userService.updateUser(user) == 1 ? "SUCCESS" : "ERROR";
    }

    @ResponseBody
    @PostMapping("deleteUser")
    public String deleteUser(@RequestBody User user){
        return userService.deleteUser(user) == 1 ? "SUCCESS" : "ERROR";
    }

    @ResponseBody
    @PostMapping("getUsersOrderBy")
    public DataGridView getUsersOrderBy(@RequestBody User user){
        return userService.getUsersOrderBy(user);
    }
}
